<?php 

class SitemapsController extends AppController{ 

    var $name = 'Sitemaps'; 
    var $uses = array('Bookmaker','Dual'); 
    var $components = array('RequestHandler'); 

    public function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('index','bookmaker','historical','live','livescore','odds');
    }

    public function index() {
		try {
			// pr($this->Session->read('parametri'));die;
						
			// CakeLog::debug("index");
			// $this->RequestHandler->respondAs('xml');
			// $this->layout='xml';
	        // $this->loadModel('Jobber');
	        $this->autorender = false;
	        // $bookmakers = $this->Bookmaker->find("all",array("conditions"=>array("deleted"=>0),"fields" => array("nome_slug","dt_ins","dt_mod"), "recursive" => -1));
			// $this->set("bookmakers",$bookmakers);

			// $query = "select concat(x.cs,'/',x.ls,'/',x.ss,'/',x.ts) path, dt_ins, dt_mod from (";
			// $query .= " select t.`country_slug` cs, t.league_slug ls, t.season_slug ss, m.localteam_slug ts, t.dt_ins dt_ins, t.dt_mod dt_mod";
			// $query .= " from tournament t inner join matchs m on t.id = m.tournament_id";
			// $query .= " union";
			// $query .= " select  t.`country_slug` cs, t.league_slug ls, t.season_slug ss, m.visitorteam_slug ts, t.dt_ins dt_ins, t.dt_mod dt_mod";
			// $query .= " from tournament t inner join matchs m on t.id = m.tournament_id";
			// $query .= " ) x";
			// // $query .= " limit 20";
			// $result = $this->Dual->query($query);
			// $this->set("historical",$result);
			// $this->set("country_historical",$this->Session->read("country_historical"));
			
			// pr($bookmakers);die;
			//debug logs will destroy xml format, make sure were not in drbug mode 
			// Configure::write ('debug', 0); 
		} catch (Exception $e) {
			CakeLog::error($e->getMessage());
		}
    } 
    
    function bookmaker() {
		try {
	        $this->autorender = false;
	        $bookmakers = $this->Bookmaker->find("all",array("conditions"=>array("deleted"=>0),"fields" => array("nome_slug","dt_ins","dt_mod"), "recursive" => -1));
			$this->set("bookmakers",$bookmakers);
		} catch (Exception $e) {
			CakeLog::error($e->getMessage());
		}
    } 
    
    function odds() {
		try {
	        $this->autorender = false;
		} catch (Exception $e) {
			CakeLog::error($e->getMessage());
		}
    } 
    
    function live() {
		try {
	        $this->autorender = false;
		} catch (Exception $e) {
			CakeLog::error($e->getMessage());
		}
    } 
    
    function livescore() {
		try {
	        $this->autorender = false;
		} catch (Exception $e) {
			CakeLog::error($e->getMessage());
		}
    } 
    
    function historical($country=null) {
		try {
			
	        $this->autorender = false;
			if (isset($country)) {
				$query = "select concat(x.cs,'/',x.ls,'/',x.ss,'/',x.ts) path from (";
				$query .= " select t.`country_slug` cs, t.league_slug ls, t.season_slug ss, m.localteam_slug ts";
				$query .= " from tournament t inner join matchs m on t.id = m.tournament_id";
				$query .= " and t.country_slug = " . StringUtil::stringToSql($country);
				$query .= " union";
				$query .= " select  t.`country_slug` cs, t.league_slug ls, t.season_slug ss, m.visitorteam_slug ts";
				$query .= " from tournament t inner join matchs m on t.id = m.tournament_id";
				$query .= " and t.country_slug = " . StringUtil::stringToSql($country);
				$query .= " ) x";
				// $query .= " limit 20";
				$result = $this->Dual->query($query);
				// pr($result);die;
				$this->set("historical",$result);
			}			
		} catch (Exception $e) {
			CakeLog::error($e->getMessage());
		}
    } 
    
	
	
} 
?>